//this is just a string
var gBookDirectory;

var book = 
{
	spineIndex: 0,
	isBookOpen: false,
	
	open: function()
	{
	    gBookDirectory = "";
		var nsIFilePicker = Components.interfaces.nsIFilePicker;
		var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
		fp.init(window, "Select a File", nsIFilePicker.modeOpen);
		fp.appendFilter("Daisy Books", "*.html");
		var res = fp.show();
		if (res == nsIFilePicker.returnOK)
		{
			if (!opennav(fp.file))
			{
				alert ("Error opening book");
				return;
			}
			this.isBookOpen = true;	
			updateMenus(true);	
	   		initPlayer();
			nccChanged(this.loadSmilUrl);
			this.spineIndex = 0;
			this.loadCurrentInSpine(false);
		}
	},

    close: function()
    {
        stopDAISYPlayback();
        closeDAISYTab();
        this.isBookOpen = false;
        updateMenus(false);
    },
	
	//smilurl is a relative path
	loadSmilUrl: function (smilurl)
	{
		if (this.isBookOpen == false) return;
		var smilfilename = stripTarget(smilurl);
		var target = getTarget(smilurl);
	
		var found = false;
		//update the spine index
		for (var i = 0; i<gSpine.length; i++)
		{
			if (gSpine[i] == smilfilename)
			{
				found = true;
				this.spineIndex = i;
				break;
			}
		}
	
		if (!found) 
		{
			alert("file not found in spine of book");
			return;
		}
	
		var smilfile = Components.classes["@mozilla.org/file/local;1"].
		            createInstance(Components.interfaces.nsILocalFile);
		smilfile.initWithFile(gNavPath.parent);
		smilfile.appendRelativePath(gSpine[this.spineIndex]);
		opensmil(smilfile);
		playAtId(target);
	},

	loadCurrentInSpine : function(startAtEnd)
	{
		var smilfile = Components.classes["@mozilla.org/file/local;1"].
		            createInstance(Components.interfaces.nsILocalFile);
		smilfile.initWithFile(gNavPath.parent);
		smilfile.appendRelativePath(gSpine[this.spineIndex]);
		opensmil(smilfile);
		if (startAtEnd) playLastPhrase();
		else playFirstPhrase();
	},

	loadNextInSpine : function()
	{
		this.spineIndex++;
		if (this.spineIndex >= gSpine.length)
		{
			alert("End of Book");
			this.spineIndex = gSpine.length - 1;
		}
		else
		{
			this.loadCurrentInSpine(false);
		}
	},

	loadPrevInSpine: function()
	{
		this.spineIndex--;
		if (this.spineIndex < 0)
		{
			alert ("Beginning of Book");
			this.spineIndex = 0;
		}
		else
		{
			this.loadCurrentInSpine(true);
		}
	}
}
